import PubSub from "pubsub-js"
import type { playModeModel, SongModel } from "../../app"
import { hasLogin } from "../../utils/permission"
import { deBounce } from "../../utils/util"
import { reqLikeSong } from "../../request/song"

const app = getApp()
let db_LikeSong: any

//播放模式
// 循环icon--lbxh 单曲icon-24gl-repeatOnce2 随机icon-24gl-shuffle
enum playModes {
  roll,
  single,
  random,
}

const modeValues: modeValue[] = ["icon--lbxh", "icon-24gl-repeatOnce2", "icon-24gl-shuffle"]

Page<DataModel, CustomOptionModel>({
  data: {
    song: {},

    current: "00:00",
    dt: "00:00",
    progress: 0,
    isPlay: false,
    playModeValue: "icon--lbxh",
  },
  onLoad() {
    //实际上在onProgress里面就可以读取到歌曲信息，但是歌曲由可能无法播放，也就是url是null，所以在挂载时也读取一次，避免player空
    const { song, isPlay, progress, playMode } = app.globalData
    this.setData({ song: { ...song }, isPlay, progress, playMode })

    PubSub.subscribe("progress", this.onProgress)

    PubSub.subscribe("pauseSong", this.onPauseSong)

    PubSub.subscribe("restartSong", this.onRestartSong)

    db_LikeSong = deBounce(reqLikeSong, 300)
  },

  onProgress(_, { time, progress, dt }) {
    const { songName, imgUrl, isLike, singers, comment } = app.globalData.song
    //在播放页内切歌时不会走挂载，所以需要在数据请求回来时，也就是progress事件的时候获取新的song数据展示
    this.setData({
      current: time,
      progress,
      dt,
      song: { ...this.data.song, songName, imgUrl, isLike, singers, comment },
    })
  },

  onPauseSong() {
    this.setData({ isPlay: false })
  },

  onRestartSong() {
    this.setData({ isPlay: true })
  },

  changeLike() {
    if (!hasLogin("先登录才能保存喜欢的歌曲哟！", false)) return
    const { isLike, songId } = this.data.song
    PubSub.publish("changeIds", isLike ? 0 : 1)
    this.setData({
      song: { ...this.data.song, isLike: !isLike },
    })
    ;(db_LikeSong as typeof reqLikeSong)(songId!, !isLike!)
  },

  changeSong({ currentTarget }) {
    PubSub.publish("changeSong", { type: currentTarget.dataset.type })
  },

  changePlay() {
    const bgm = wx.getBackgroundAudioManager()
    const { isPlay } = this.data
    if (isPlay) {
      PubSub.publish("pauseSong", false)
    } else {
      //当歌曲播放错误的时候，就会变成空串
      app.globalData.songUrl === "" ? PubSub.publish("changeSong", { type: "next" }) : bgm.play()
    }
    this.setData({ isPlay: !isPlay })
  },

  changeMode() {
    const playMode: playModeModel = app.globalData.playMode
    const next = playModes[playMode] === 2 ? 0 : playModes[playMode] + 1
    const newMode = playModes[next]
    PubSub.publish("changeMode", newMode)
    this.setData({ playModeValue: modeValues[next] })
  },

  goBack() {
    wx.navigateBack()
  },

  onUnload() {
    PubSub.unsubscribe(this.onProgress)
    PubSub.unsubscribe(this.onPauseSong)
    PubSub.unsubscribe(this.onRestartSong)
  },
})

type modeValue = "icon--lbxh" | "icon-24gl-repeatOnce2" | "icon-24gl-shuffle"

interface DataModel {
  song: Partial<SongModel>

  current: string
  dt: string
  progress: number
  isPlay: boolean
  playModeValue: modeValue
}

interface CustomOptionModel {
  onProgress: (msg: string, playload: { time: string; progress: number; dt: string }) => void
  onPauseSong: () => void
  onRestartSong: () => void

  changeLike: () => void
  changeSong: (e: WechatMiniprogram.Touch) => void
  changePlay: () => void
  changeMode: () => void
  goBack: () => void
}

// type Instance = WechatMiniprogram.Page.Instance<DataModel, CustomOptionModel>
